package com.iuie.ns.system.controller.report;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.iuie.ns.frame.web.dataset.vo.Page;
import com.iuie.ns.frame.web.response.vo.ResponseInfo;
import com.iuie.ns.system.service.report.IReportInfoService;
import com.iuie.ns.system.service.report.IReportParamsService;
import com.iuie.ns.system.vo.report.ReportInfoVO;
import com.iuie.ns.system.vo.report.ReportParamsVO;

/**
 * 系统报表管理controller
 * @author liujie
 * @date 2018年5月19日下午4:33:50
 */
@Controller
@RequestMapping("report")
public class ReportController {
	
	@Resource
	private IReportInfoService reportService;
	
	@Resource
	private IReportParamsService reportParamsService;

	/**
	 * 报表管理index
	 * @return
	 * @date 2018年5月19日下午4:34:36
	 */
	@RequestMapping
	public String index() {
		return "view-system/report/report";
	}
	
	/**
	 * 新增报表信息页面
	 * @return
	 * @date 2018年5月19日下午5:00:43
	 */
	@RequestMapping(value = "add/index")
	public String addIndex() {
		return "view-system/report/reportAdd";
	}
	
	/**
	 * 新增报表参数页面
	 * @param reportId
	 * @param request
	 * @return
	 * @date 2018年5月21日上午10:13:20
	 */
	@RequestMapping(value = "params/add/{reportId}/index")
	public String addParamsIndex(@PathVariable("reportId") String reportId, HttpServletRequest request) {
		request.setAttribute("reportId", reportId);
		return "view-system/report/reportParamsAdd";
	}
	
	/**
	 * 修改报表信息页面
	 * @param reportId
	 * @param request
	 * @return
	 * @date 2018年5月19日下午5:02:46
	 */
	@RequestMapping(value = "update/{reportId}/index")
	public String updateIndex(@PathVariable("reportId") String reportId, HttpServletRequest request) {
		request.setAttribute("reportId", reportId);
		return "view-system/report/reportAdd";
	}
	
	/**
	 * 更新报表参数-index
	 * @param reportId
	 * @param paramKey
	 * @param request
	 * @return
	 * @date 2018年5月21日上午10:28:07
	 */
	@RequestMapping(value = "params/update/{reportId}/{paramKey}/index")
	public String updateParamsIndex(@PathVariable("reportId") String reportId, @PathVariable("paramKey") String paramKey, HttpServletRequest request) {
		request.setAttribute("reportId", reportId);
		request.setAttribute("paramKey", paramKey);
		return "view-system/report/reportParamsAdd";
	}
	
	/**
	 * 分页查询报表信息
	 * @param page
	 * @param condition
	 * @return
	 * @date 2018年5月19日下午4:50:21
	 */
	@ResponseBody
	@RequestMapping(value = "find/page", method = RequestMethod.GET)
	public ResponseInfo findByPage(Page page, ReportInfoVO condition) {
		List<ReportInfoVO> list = reportService.findByPage(page, condition);
		return ResponseInfo.successInfo(list, page);
	}
	
	/**
	 * 新增报表信息
	 * @param item
	 * @return
	 * @date 2018年5月19日下午5:42:09
	 */
	@ResponseBody
	@RequestMapping(value = "add/one", method = RequestMethod.POST)
	public ResponseInfo addOne(ReportInfoVO item) {
		reportService.addOne(item);
		return ResponseInfo.successInfo("保存成功！");
	}
	
	/**
	 * 新增单条报表参数
	 * @param item
	 * @return
	 * @date 2018年5月21日上午9:58:23
	 */
	@ResponseBody
	@RequestMapping(value = "params/add/one", method = RequestMethod.POST)
	public ResponseInfo addOneParams(ReportParamsVO item, String oldParamKey) {
		reportParamsService.addOne(item, oldParamKey);
		return ResponseInfo.successInfo("保存成功！");
	}
	
	/**
	 * 根据ID删除单条记录
	 * @param reportId
	 * @return
	 * @date 2018年5月20日下午8:37:25
	 */
	@ResponseBody
	@RequestMapping(value = "delete/id/{reportId}", method = RequestMethod.POST)
	public ResponseInfo deleteById(@PathVariable("reportId") String reportId) {
		reportService.deleteById(reportId);
		return ResponseInfo.successInfo("删除成功！");
	}
	
	/**
	 * 根据reportId和paramKey删除参数
	 * @param reportId
	 * @param paramKey
	 * @return
	 * @date 2018年5月21日上午8:43:50
	 */
	@ResponseBody
	@RequestMapping(value = "params/delete/paramKey/{reportId}/{paramKey}/one", method = RequestMethod.POST)
	public ResponseInfo deleteParamByReportIdAndParamKey(@PathVariable("reportId") String reportId, @PathVariable("paramKey") String paramKey) {
		reportParamsService.deleteParamByReportIdAndParamKey(reportId, paramKey);
		return ResponseInfo.successInfo("删除成功！");
	}
	
	/**
	 * 根据Id查询单条记录
	 * @param reportId
	 * @return
	 * @date 2018年5月20日下午8:37:01
	 */
	@ResponseBody
	@RequestMapping(value = "find/id/{reportId}", method = RequestMethod.GET)
	public ResponseInfo findById(@PathVariable("reportId") String reportId) {
		ReportInfoVO reportInfo = reportService.findById(reportId);
		return ResponseInfo.successInfoByObject(reportInfo);
	}
	
	/**
	 * 查询报表参数
	 * @param page
	 * @param condition
	 * @return
	 * @date 2018年5月20日下午9:22:27
	 */
	@ResponseBody
	@RequestMapping(value = "params/find/page", method = RequestMethod.GET)
	public ResponseInfo findParamsByPage(Page page, ReportParamsVO condition) {
		List<ReportParamsVO> list = reportParamsService.findByPage(page, condition);
		return ResponseInfo.successInfo(list, page);
	}
	
	
	/**
	 * 根据reportId和paramKey查询单条记录
	 * @param reportId
	 * @param paramKey
	 * @return
	 * @date 2018年5月21日上午10:40:07
	 */
	@ResponseBody
	@RequestMapping(value = "params/find/id/{reportId}/{paramKey}/one", method = RequestMethod.GET)
	public ResponseInfo findParamsById(@PathVariable("reportId") String reportId, @PathVariable("paramKey") String paramKey) {
		ReportParamsVO reportParams = reportParamsService.findById(reportId, paramKey);
		return ResponseInfo.successInfoByObject(reportParams);
	}
	
	/**
	 * 获取报表参数键值对
	 * @param reportId
	 * @return
	 * @date 2018年5月22日上午10:09:26
	 */
	@ResponseBody
	@RequestMapping(value = "params/find/map/{reportId}", method = RequestMethod.GET)
	public ResponseInfo findMapOfParams(@PathVariable("reportId") String reportId) {
		Map<String, String> params = reportParamsService.findMapOfParams(reportId);
		return ResponseInfo.successInfoByObject(params);
	}
	
	/**
	 * 
	 * @return
	 * @date 2018年5月22日上午10:11:29
	 */
	@ResponseBody
	@RequestMapping(value = "json/find/{reportCategory}/{reportKey}/json", method = RequestMethod.POST)
	public ResponseInfo findReportJson(@PathVariable("reportKey") String reportKey, 
			@PathVariable("reportCategory") String reportCategory, HttpServletRequest request) {
		Map<String, String> map = reportService.findReportJson(reportCategory, reportKey, request);
		return ResponseInfo.successInfoByObject(map);
	}
}
